﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c) 2011
*/
using System;
using System.ComponentModel;
using System.Data;
using NetBay.Core.Log;
using NetBay.Core.DataAccess;
using NetBay.Core.Extensions;
using NetBay.Core.BusinessEntities.TaskManager;

namespace Rio.Manager.DataAccess
{
    public class DA_TaskManager
    {
        #region Enums
        public enum DbFunction
        {
            [DescriptionAttribute("")]

            None,
            [DescriptionAttribute("P_TASK_Get")]

            GetTaskManager,
            [DescriptionAttribute("P_TASK_Add")]

            AddTaskManager,
            [DescriptionAttribute("P_TASK_Delete")]

            DeleteTaskManager,
            [DescriptionAttribute("P_TASK_Delete_All")]

            DeleteAllTaskManager,
            [DescriptionAttribute("P_TASK_Update")]

            UpdateTaskManager,
        }

        #endregion

        #region Function

        /// <summary>
        /// Execute P_TASK_Get to get the task manager list.
        /// </summary>
        /// <returns></returns>
        public static IDataReader GetTaskManager()
        {
            try
            {
                LogManager.Instance().LogDebug("Da_TaskManager.GetTaskManager");

                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.GetTaskManager.szGetDescription());

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("Da_TaskManager --> GetTaskManager exception[" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Execute P_TASK_Add to add an task manager.
        /// </summary>
        /// <param name="p_oTaskToAdd">Task to add</param>
        /// <returns></returns>
        public static void AddTaskManager(TaskItem p_oTaskToAdd)
        {
            try
            {
                LogManager.Instance().LogDebug("Da_TaskManager.AddTaskManager: " + p_oTaskToAdd.ToString());

                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.AddTaskManager.szGetDescription(), false);
                db.AddInParameter(dbCommand, "p_iMachineId", DbType.Int32, p_oTaskToAdd.MachineId);
                db.AddInParameter(dbCommand, "p_sAction", DbType.String, Convert.ToString(Convert.ToInt32(p_oTaskToAdd.Action)));
                db.AddInParameter(dbCommand, "p_sStartTime", DbType.String, p_oTaskToAdd.StartTime.ToString(@"hh\:mm"));
                db.AddInParameter(dbCommand, "p_sEndTime", DbType.String, p_oTaskToAdd.EndTime.ToString(@"hh\:mm"));
                db.AddInParameter(dbCommand, "p_iMonday", DbType.Int32, Convert.ToInt32(p_oTaskToAdd.Monday));
                db.AddInParameter(dbCommand, "p_iTuesday", DbType.Int32, Convert.ToInt32(p_oTaskToAdd.Tuesday));
                db.AddInParameter(dbCommand, "p_iWednesday", DbType.Int32, Convert.ToInt32(p_oTaskToAdd.Wednesday));
                db.AddInParameter(dbCommand, "p_iThursday", DbType.Int32, Convert.ToInt32(p_oTaskToAdd.Thursday));
                db.AddInParameter(dbCommand, "p_iFriday", DbType.Int32, Convert.ToInt32(p_oTaskToAdd.Friday));
                db.AddInParameter(dbCommand, "p_iSaturday", DbType.Int32, Convert.ToInt32(p_oTaskToAdd.Saturday));
                db.AddInParameter(dbCommand, "p_iSunday", DbType.Int32, Convert.ToInt32(p_oTaskToAdd.Sunday));
                
                db.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_TaskManager --> AddTaskManager exception[" + ex.Message + "]");
            }
        }

        /// <summary>
        /// Execute P_TASK_Delete to delete task manager.
        /// </summary>
        /// <param name="p_oTaskToDelete">Task to delete</param>
        /// <returns></returns>
        public static void DeleteTaskManager(TaskItem p_oTaskToDelete)
        {
            try
            {
                LogManager.Instance().LogDebug("Da_TaskManager.DeleteTaskManager: " + p_oTaskToDelete.ToString());

                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.DeleteTaskManager.szGetDescription(), false);
                db.AddInParameter(dbCommand, "p_sAction", DbType.String, Convert.ToString(Convert.ToInt32(p_oTaskToDelete.Action)));
                db.AddInParameter(dbCommand, "p_sStartTime", DbType.String, p_oTaskToDelete.StartTime.ToString(@"hh\:mm"));             

                db.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_TaskManager --> DeleteTaskManager exception[" + ex.Message + "]");
            }
        }

        /// <summary>
        /// Execute P_TASK_Delete_All to delete all the task manager.
        /// </summary>
        /// <param name="p_oTaskToDelete">Delete all the task</param>
        /// <returns></returns>
        public static void DeleteAllTaskManager()
        {
            try
            {
                LogManager.Instance().LogDebug("Da_TaskManager.DeleteAllTaskManager: ");

                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.DeleteAllTaskManager.szGetDescription());
                db.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_TaskManager --> DeleteAllTaskManager exception[" + ex.Message + "]");
            }
        }

        /// <summary>
        /// Execute P_TASK_Update to update task manager list.
        /// </summary>
        /// <param name="p_oTaskToUpdate">Task to update</param>
        /// <param name="p_sOldAction">Old action value if it's modify</param>
        /// <param name="p_sOldStartHour">Old start hour if it's modify</param> 
        /// <returns></returns>
        public static void UpdateTaskManager(TaskItem p_oTaskToUpdate, string p_sOldAction, string p_sOldStartHour)
        {
            try
            {
                LogManager.Instance().LogDebug("Da_TaskManager.UpdateTaskManager: " + p_oTaskToUpdate.ToString());

                var db = DbHelper.DataBase;
                string l_sValueHourToTest = "00:00";

                var dbCommand = db.GetNewCommand(DbFunction.UpdateTaskManager.szGetDescription(), false);

                //Two parameters for updating good task --> Not primary key in r_taskmanager
                if (p_sOldAction == "")
                {
                    db.AddInParameter(dbCommand, "p_sOldAction", DbType.String, Convert.ToString(Convert.ToInt32(p_oTaskToUpdate.Action)));
                }
                else
                {
                    db.AddInParameter(dbCommand, "p_sOldAction", DbType.String, Convert.ToString(Convert.ToInt32((TaskType)Enum.Parse(typeof(TaskType), p_sOldAction.ToString()))));
                }
                if (p_sOldStartHour == l_sValueHourToTest)
                {
                    db.AddInParameter(dbCommand, "p_sStartTime", DbType.String, p_oTaskToUpdate.StartTime.ToString(@"hh\:mm"));
                }
                else
                {
                    db.AddInParameter(dbCommand, "p_sStartTime", DbType.String, p_sOldStartHour.ToString(@"hh\:mm"));
                }

                //Data to update.
                db.AddInParameter(dbCommand, "p_iMachineId", DbType.Int32, p_oTaskToUpdate.MachineId);
                db.AddInParameter(dbCommand, "p_sAction", DbType.String, Convert.ToString(Convert.ToInt32(p_oTaskToUpdate.Action)));
                db.AddInParameter(dbCommand, "p_sStartTime", DbType.String, p_oTaskToUpdate.StartTime.ToString(@"hh\:mm"));
                db.AddInParameter(dbCommand, "p_sEndTime", DbType.String, p_oTaskToUpdate.EndTime.ToString(@"hh\:mm"));
                db.AddInParameter(dbCommand, "p_iMonday", DbType.Int32, Convert.ToInt32(p_oTaskToUpdate.Monday));
                db.AddInParameter(dbCommand, "p_iTuesday", DbType.Int32, Convert.ToInt32(p_oTaskToUpdate.Tuesday));
                db.AddInParameter(dbCommand, "p_iWednesday", DbType.Int32, Convert.ToInt32(p_oTaskToUpdate.Wednesday));
                db.AddInParameter(dbCommand, "p_iThursday", DbType.Int32, Convert.ToInt32(p_oTaskToUpdate.Thursday));
                db.AddInParameter(dbCommand, "p_iFriday", DbType.Int32, Convert.ToInt32(p_oTaskToUpdate.Friday));
                db.AddInParameter(dbCommand, "p_iSaturday", DbType.Int32, Convert.ToInt32(p_oTaskToUpdate.Saturday));
                db.AddInParameter(dbCommand, "p_iSunday", DbType.Int32, Convert.ToInt32(p_oTaskToUpdate.Sunday));
                
                db.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_TaskManager --> UpdateTaskManager exception[" + ex.Message + "]");
            }
        }
        #endregion
    }
}